<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-upload
        class="avatar-uploader"
        action="#"
        list-type="picture-card"
        :http-request="uploadFn"
        :limit="1"
        :file-list="fileList"
        :class="{disable:fileList.length>0}"
        :on-remove="removeFn"
      >
        <!-- <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon" /> -->
        <i class="el-icon-plus avatar-uploader-icon" />
      </el-upload>
      <el-progress
        v-if="percent>0&&percent<100"
        type="circle"
        :percentage="percent"
      />
    </div>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
// SECRETID 和 SECRETKEY请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
const cos = new COS({
  SecretId: 'AKID4wYv4xaw2sSmpdBSTEjxvfAPN5XjeJiq',
  SecretKey: 'tWt0rREl22mGTQ6bqoVw72g4E8l8F5fy'
})
export default {
  data() {
    return {
      imageUrl: '../../assets/common/bigUserHeader.png',
      fileList: [],
      percent: 0
    }
  },
  methods: {
    removeFn() {
      this.fileList = []
    },
    uploadFn(data) {
      this.fileList = [{ url: '' }]
      console.log('上传', data)
      cos.putObject({
        Bucket: 'hr-1314002095', /* 必须 */
        Region: 'ap-guangzhou', /* 存储桶所在地域，必须字段 */
        Key: Date.now().toString(), /* 必须 */
        StorageClass: 'STANDARD',
        Body: data.file, // 上传文件对象
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))
          this.percent = progressData.percent * 100
        }
      }, (err, data) => {
        console.log('上传图片', err, data)
        if (!err) {
          // 图片上传成功
          this.fileList[0].url = 'https://' + data.Location
        }
      })
    }
  }
}
</script>

    <style scoped lang="scss">
        .app-container {
            display: inline-block;
            .disable {
              ::v-deep .el-upload--picture-card {
                display: none;
              }
            }
        }
        .el-progress {
          position: absolute;
          top: 44%;
          left: 31%;
          transform: translate(-50%,-50%);
        }
    </style>

